﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class MapGridBuilder : MonoBehaviour
{

    [SerializeField]
    private SpriteRenderer _map;

    private Sprite _grid_background;
    private SpriteRenderer _grid;

    private void Awake ()
    {
        _grid = GetComponent<SpriteRenderer>();

        if (null == _map || null == _map.sprite)
        {
            throw new MapNullException( "Map is null or Map's sprite is null" );
        }

        var width = _map.sprite.texture.width;
        var height = _map.sprite.texture.height;

        var bounds = _map.bounds;

        _grid_background = Sprite.Create( new Texture2D( width, height ), new Rect( 0, 0, width, height ), new Vector2( 0, 0 ) );
        _grid_background.name = "Grid Background";

        _grid.sprite = _grid_background;
        _grid.transform.localPosition = new Vector3()
        {
            x = _map.transform.localPosition.x,
            y = _map.transform.localPosition.y,
            z = _map.transform.localPosition.z - 10
        };

        //var bounds = _map.bounds;

        //Camera.main.orthographic = true;
        //Camera.main.orthographicSize = bounds.size.y * Screen.height / Screen.width * 0.5f;
    }

    private void Start ()
    {

    }

    private void Update ()
    {

    }

    public class MapNullException : System.Exception
    {
        public MapNullException(string msg)
            : base(msg) { }
    }
}
